/**
 * Class for managing connections to the server.
 * Currently based on an array list. This should
 * definately be changed to a more efficient structure.
 * @author : bigredshoes03
 * @updated : 9/30/2009
 */
package main_serv;

import java.util.ArrayList;

import user.Session;

public class ClientHandler {
	private static ClientHandler unique;
	private ArrayList<Session> s;
	private ClientHandler(){
		s = new ArrayList<Session>();
	}
	
	/**
	 * Adds a Session to the list of
	 * current sessions
	 * @param s - session to add
	 */
	public void add(Session s){
		this.s.add(s);
	}
	
	/**
	 * Remove a session from the current
	 * list of sessions
	 * @param s - session object to remove
	 */
	public void remove(Session s){
		this.s.remove(s);
	}
	
	/**
	 * Remove a session from the current list
	 * of sessions via session ID
	 * @param clientId - id of client to remove
	 */
	public void remove(int clientId){
		for(int i=0;i<s.size();i++){
			Session p = s.get(i);
			if(clientId==p.getID()){
				s.remove(i);
				break;
			}
		}
	}
	
	/**
	 * Propegate the update command to
	 * all connected users
	 */
	public void update(){
		for(Session ses : s){
			ses.update();
		}
	}
	
	/**
	 * Allow singleton access
	 * @return - Server's Client Handler
	 */
	public static ClientHandler getInstance(){
		if(unique == null){
			synchronized(new Object()){
				if(unique == null){
					unique = new ClientHandler();
				}
			}
		}
		return unique;
	}

}
